package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.wms.dto.IncreaseDecreaseDTO;
import com.itheima.wms.entity.IncreaseDecreaseEntity;
import com.itheima.wms.service.IncreaseDecreaseService;
import com.itheima.wms.vo.IncreaseDecreaseDetailVO;
import com.itheima.wms.vo.IncreaseDecreaseSumVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 损益单
 * </p>
 */
@Slf4j
@RestController
@RequestMapping("/increaseDecrease")
@Api(value = "IncreaseDecrease", tags = "损益单")
public class IncreaseDecreaseController extends BaseController {

    @Autowired
    private IncreaseDecreaseService increaseDecreaseService;

    /**
     * 分页查询损益单
     *
     * @param data 分页查询对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询损益单", notes = "分页查询损益单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "code", value = "损益单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "stockId", value = "库存", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "idNum", value = "损益数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "idMoney", value = "损益金额", dataType = "java.math.BigDecimal", paramType = "query"),
            @ApiImplicitParam(name = "idSource", value = "损益来源 盘点:PD、上架:SJ、拣货:JH", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "taskCode", value = "任务单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "损益单状态 1.新建、2已调整、3已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/page")
    public R<IPage<IncreaseDecreaseEntity>> page(@RequestParam Map data) {
        Page<IncreaseDecreaseEntity> page = getPage();
        IPage<IncreaseDecreaseEntity> increaseDecreasePage = increaseDecreaseService.page(page, data);
        page.setRecords(increaseDecreasePage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "分页查询损益单明细", notes = "分页查询损益单明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示几条", dataType = "long", paramType = "query", defaultValue = "10"),
            @ApiImplicitParam(name = "code", value = "损益单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "goodsName", value = "货品名称", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "ownerName", value = "货主名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "损益单状态 1.新建、2已调整、3已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageDetail")
    public R<IPage<IncreaseDecreaseDetailVO>> pageDetail(@RequestParam Map data) {
        Page<IncreaseDecreaseDetailVO> page = getPage();
        IPage<IncreaseDecreaseDetailVO> increaseDecreaseDetailVOIPage = increaseDecreaseService.pageDetail(page, data);
        page.setRecords(increaseDecreaseDetailVOIPage.getRecords());
        return success(page);
    }

    @ApiOperation(value = "查询全部损益单", notes = "查询全部损益单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ascs", value = "正序排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "descs", value = "倒叙排列字段（逗号分隔）", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "data", value = "无效参数 字段可以使用前缀控制查询方式 gt_, ge_, lt_, le_, in_, like_, likeRight_, likeLeft_", dataType = "string", paramType = "none"),
            @ApiImplicitParam(name = "code", value = "损益单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "stockId", value = "库存", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "idNum", value = "损益数量", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "idMoney", value = "损益金额", dataType = "java.math.BigDecimal", paramType = "query"),
            @ApiImplicitParam(name = "idSource", value = "损益来源 盘点:PD、上架:SJ、拣货:JH", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "taskCode", value = "任务单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "损益单状态 1.新建、2已调整、3已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createUser", value = "创建人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "createName", value = "创建人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "createTime", value = "创建时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "updateUser", value = "更新人id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "updateName", value = "更新人", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "updateTime", value = "更新时间", dataType = "java.time.LocalDateTime", paramType = "query"),
            @ApiImplicitParam(name = "logicDel", value = "逻辑删除 0 删除", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/list")
    public R<List<IncreaseDecreaseEntity>> list(@RequestParam Map data) {
        List<IncreaseDecreaseEntity> increaseDecreaseArray = increaseDecreaseService.list(data);
        return success(increaseDecreaseArray);
    }

    @ApiOperation(value = "查询损益单明细", notes = "查询损益单明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "损益单号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "goodsName", value = "货品名称", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "ownerName", value = "货主名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "损益单状态 1.新建、2已调整、3已取消", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "taskId", value = "任务id", dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/listDetail")
    public R<List<IncreaseDecreaseDetailVO>> listDetail(@RequestParam Map data) {
        List<IncreaseDecreaseDetailVO> list = increaseDecreaseService.listDetail(data);
        return success(list);
    }


    @ApiOperation(value = "汇总总数与总金额", notes = "汇总总数与总金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "汇总总数，总金额", dataType = "string", paramType = "none"),
    })
    @GetMapping("/sum")
    public R<IncreaseDecreaseSumVO> sum(@RequestParam Map data) {
        IncreaseDecreaseSumVO increaseDecreaseSumVO = increaseDecreaseService.sum(data);
        return success(increaseDecreaseSumVO);
    }

    /**
     * 查询损益单
     *
     * @param id 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "查询损益单", notes = "查询损益单")
    @GetMapping("/{id}")
    public R<IncreaseDecreaseEntity> get(@PathVariable Long id) {
        return success(increaseDecreaseService.getById(id));
    }

    /**
     * 新增损益单
     *
     * @param data 新增对象
     * @return 新增结果
     */
    @ApiOperation(value = "新增损益单", notes = "新增损益单不为空的字段")
    @PostMapping
    public R<IncreaseDecreaseEntity> save(@RequestBody @Validated(SuperEntity.Save.class) IncreaseDecreaseDTO data) {
        increaseDecreaseService.save(data);
        return success(data);
    }

    /**
     * 修改损益单
     *
     * @param data 修改对象
     * @return 修改结果
     */
    @ApiOperation(value = "修改损益单", notes = "修改损益单不为空的字段")
    @PutMapping
    public R<IncreaseDecreaseEntity> update(@RequestBody @Validated(SuperEntity.Update.class) IncreaseDecreaseDTO data) {
        increaseDecreaseService.updateById(data);
        return success(data);
    }

    /**
     * 调整库存
     *
     * @param data 调整库存
     * @return 修改结果
     */
    @ApiOperation(value = "调整库存", notes = "调整库存")
    @PutMapping("change")
    public R<Boolean> change(@RequestBody IncreaseDecreaseDTO data) {
        increaseDecreaseService.change(data);
        return success(true);
    }

    /**
     * 取消调整
     *
     * @param data 取消调整
     * @return 修改结果
     */
    @ApiOperation(value = "取消调整", notes = "取消调整")
    @PutMapping("cancel")
    public R<Boolean> cancel(@RequestBody IncreaseDecreaseDTO data) {
        increaseDecreaseService.cancel(data);
        return success(true);
    }

    /**
     * 删除损益单
     *
     * @param ids 主键id
     * @return 删除结果
     */
    @ApiOperation(value = "删除损益单", notes = "根据id物理删除损益单")
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        increaseDecreaseService.removeByIds(ids);
        return success();
    }

}
